import { getuser } from '../../utils/storage'
import { request, wxlogin } from '../../utils/https'
import { sortBy, groupBy } from 'underscore'
import * as moment from 'moment'
import {
  getschoolclazzschedule,
  getschoolclazzstudentcalendar
} from '../../utils/interfaces'

export default {
  name: 'index',
  data () {
    const time = new Date(new Date().toLocaleDateString())
    return {
      logStatus: 0,
      movies: [
        {
          url:
            'https://hterobot-kimage.oss-cn-qingdao.aliyuncs.com/images1/banner@2x.png'
        },
        {
          url:
            'https://hterobot-kimage.oss-cn-qingdao.aliyuncs.com/images1/banner@2x.png'
        },
        {
          url:
            'https://hterobot-kimage.oss-cn-qingdao.aliyuncs.com/images1/banner@2x.png'
        }
      ],
      current: 'tab1',
      currentPage: 1,
      currentpa: 1,
      schedule: [],
      calendar: [],
      newtime: time.toLocaleDateString(),
      forstime: new Date(
        time.getTime() - 1000 * 3500 * 24
      ).toLocaleDateString(),
      weeks: ['日', '一', '二', '三', '四', '五', '六']
    }
  },
  computed: {
    getschedule () {
      const { schedule, src, weeks } = this
      for (let v of schedule) {
        v.img = src(v.schoolclasstime)
        v.text = moment(v.starttime).format('YYYY年MM月DD日') + ' 星期' + weeks[new Date(v.starttime).getDay()]
        if (v.schoolclazzstudentcalendar.length) {
          v.type = v.schoolclazzstudentcalendar[0].type
        }
      }
      let data = schedule.filter(v => v.type !== '上课')
      data = groupBy(sortBy(data, v => {
        return (new Date(v.starttime))
      }), 'starttime')
      return data
    },
    getcalendar () {
      const { calendar, src, weeks } = this
      for (let v of calendar) {
        v.img = src(v.schoolclazzschedule.schoolclasstime)
        v.text = moment(v.schoolclazzschedule.starttime).format('YYYY年MM月DD日') + ' 星期' + weeks[new Date(v.schoolclazzschedule.starttime).getDay()]
      }
      const data = groupBy(sortBy(calendar, v => {
        return -(new Date(v.schoolclazzschedule.starttime))
      }), v => {
        return v.schoolclazzschedule.starttime
      })
      return data
    }
  },
  onShow () {
    this.start()
  },
  methods: {
    async start () {
      try {
        const { ID } = await getuser('user')
        if (!ID) {
          wx.navigateTo({
            url: '/pages/home/main'
          })
        }
        let code = await wxlogin()
        this.schedule = await request(getschoolclazzschedule, {
          data: {
            schoolstudentid: ID,
            code
          }
        })
        code = await wxlogin()
        this.calendar = await request(getschoolclazzstudentcalendar, {
          data: {
            schoolstudentid: ID,
            code
          }
        })
      } catch (err) {
        wx.showModal({
          title: '提示',
          content: '服务器请求失败，点击确定重新加载',
          success: res => {
            if (res.confirm) {
              this.start()
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
    },
    upke (ID) {
      wx.navigateTo({
        url: `../../pages/webview/main?ID=${ID}`
      })
    },
    src (time) {
      const [a] = time.split('-')
      const [c] = a.split(':')
      const num = Number(c)
      return `https://hterobot-kimage.oss-cn-qingdao.aliyuncs.com/images1/course_${num}.png`
    },
    isshow (newtime, index) {
      return new Date(index).toLocaleDateString() === new Date(newtime).toLocaleDateString()
    },
    handleChange ({target}) {
      // this.current = target.name
      // console.log(this.current)
    }
  }
}
